package com.adong.juc;

import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch 减少计数
 * 解释：6个同学陆续离开教室后值班同学才可以关门。
 * main主线程必须要等前面6个线程完成全部工作后，自己才能开干
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch cd = new CountDownLatch(6);

        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {

                System.out.println(Thread.currentThread().getName() + "\t" + "号离开教师");
                cd.countDown(); // 一定要放到最后
            }, String.valueOf(i)).start();

        }

        cd.await();
        System.out.println(Thread.currentThread().getName() + "\t 班长离开锁门");
    }
}
